//
//  SYRootViewController.m
//  MicroNews
//
//  Created by tongcheng on 15/10/20.
//  Copyright © 2015年 siyue. All rights reserved.
//

#import "SYRootViewController.h"
#import "SYLeftMenuView.h"
#import "SYHomeController.h"
#import "SYBaseNavController.h"
#import "SYMessageController.h"
#import "SYShowNewsController.h"
#import "SYLeftViewController.h"


//定义左边菜单栏的宽、高 y
#define LeftMenuW    ScreenWidth*0.65

#define Timer 0.25
//覆盖层按钮的tag
#define buttonTag 1200

@interface SYRootViewController ()<SYLeftMenuViewDelegate>

//左边菜单栏
@property (nonatomic,weak)  SYLeftMenuView *leftMenu;

//当前显示的控制器
@property (nonatomic,strong) SYBaseNavController *showNavController;

@property (nonatomic, strong) SYLeftViewController *leftVc;


@end

@implementation SYRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加背景图片
    [self addBackgroundImage];
    
    // 添加左边菜单栏
    [self setupLeftMenu];
    
    // 添加子控制器
    [self addChildViewControllers];
    
    
}

#pragma mark - 添加背景图片
-(void)addBackgroundImage
{
    UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:self.view.bounds];
    backgroundImage.image = [UIImage imageNamed:@"sidebar_bg@2x.jpg"];
    [self.view addSubview:backgroundImage];
}

//#pragma mark - 添加左边用户信息
//-(void)setupLeftMenu
//{
//    SYLeftMenuView *leftMenu = [[SYLeftMenuView alloc] initWithFrame:CGRectMake(0, 0,LeftMenuW+20,ScreenHeight)];
//    [self.view insertSubview:leftMenu atIndex:1];
//    leftMenu.delegate = self;
//    self.leftMenu = leftMenu;
//}

#pragma mark 添加左边的控制器
-(void)setupLeftMenu
{
    SYLeftViewController *left=[[SYLeftViewController alloc]init];

    
    left.view.x = 0;
    left.view.y = 0;
    left.view.width = LeftMenuW+20;
    left.view.height = ScreenHeight;
    [self.view insertSubview:left.view atIndex:1];
//    //头像下面菜单的点击代理
//    right.topMenu.delegete=self;
//    //右边中间按钮视图的代理
//    right.centreView.delegate=self;
//    //右边底部按钮的代理
//    right.footListView.delegate=self;
    
    left.leftMenu.delegate=self; //头像点击的代理
    
    self.leftVc=left;
}

#pragma mark - 添加子控制器
-(void)addChildViewControllers
{
    // 首页
    SYHomeController *home = [[SYHomeController alloc] init];
    [self setupController:home title:@"新闻"];
    
    //2.添加控制器
//    SYMessageController *message=[[SYMessageController alloc]init];
    UIStoryboard *storyboardTwe = [UIStoryboard storyboardWithName:@"Message" bundle:nil];
    SYMessageController *message = [storyboardTwe instantiateViewControllerWithIdentifier:@"MessageVC"];

    [self setupController:message title:@"消息"];
    //3.
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"News" bundle:nil];
    SYShowNewsController *collection = [storyboard instantiateViewControllerWithIdentifier:@"MainNewsVC"];
    [self setupController:collection title:@"收藏"];
    //4.
    UIViewController *set=[[UIViewController alloc]init];
    set.view.backgroundColor = SYRandomColor;
    [self setupController:set title:@"设置"];
    //5.
    UIViewController *setting=[[UIViewController alloc]init];
    setting.view.backgroundColor = SYRandomColor;
    [self setupController:setting title:@"跟设置"];

}

#pragma mark -- 添加子控制器详细方法
-(void)setupController:(UIViewController *)vc title:(NSString *)title
{
    if(self.childViewControllers.count>=1){
//        vc.view.backgroundColor=SYRandomColor;
    }
    
    SYBaseNavController *nav = [[SYBaseNavController alloc] initWithRootViewController:vc];
    vc.title = title;
    vc.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithLeftIcon:@"User_" highIcon:@"" target:self action:@selector(leftClick)];
    [self addChildViewController:nav];
    
    //如果是首页 则添加首页View
    if (self.childViewControllers.count == 1) {
        self.showNavController = nav;
        [self.view addSubview:nav.view];
    }

}

-(void)leftClick
{
    //4.创建一个按钮覆盖首页
    UIButton *cover=[[UIButton alloc]initWithFrame:self.showNavController.view.bounds];
    cover.backgroundColor = [UIColor blackColor];
    cover.alpha = 0.1;

//    self.leftMenu.hidden=NO;
//    self.rightVc.view.hidden=YES;
    //1.先调整左边菜单的动画属性   然后在下面的block里面 在调回去
    CGAffineTransform scaleform=CGAffineTransformMakeScale(1, 1);
    CGAffineTransform anim=CGAffineTransformTranslate(scaleform, -80, 0);
    self.leftMenu.transform=anim;
    
    //2.
    
    /*
     获得CGAffineTransform有多种方法，例如使用CGAffineTransformMake，
     但是对于矩阵操作相对比较麻烦，事实上iOS已经为我们准备好了三个方法：
             CGAffineTransformMakeRotation（旋转）
             CGAffineTransformMakeScale（缩放）
             CGAffineTransformMakeTranslation（移动）
     transform进行旋转、缩放、移动的时候不是在原来的基础上增量形变的，
     因此如果需要持续在原来的基础上旋转、缩放、移动那么每次需要在原来的基础上增加或减少。
     当然，我们可以定义一个全局变量进行累加，但是事实上iOS已经为我们提供好了三个对应的方法，分别用于在原来的角度、缩放、移动位置的基础上做出修改：CGAffineTransformRotate、CGAffineTransformScale、CGAffineTransformTranslate；
     
     */
    [UIView animateWithDuration:Timer animations:^{
        //调回左边菜单的动画属性
        self.leftMenu.transform=CGAffineTransformIdentity;
        //1.算出缩放比例
        CGFloat navH=ScreenHeight-2;
        CGFloat scale=navH/ScreenHeight; //比例
        //2.左边菜单的距离
        CGFloat leftMargin=ScreenWidth*(1-scale)*0.5; //缩放后首页视图距离两边的距离
        CGFloat translateX=(LeftMenuW-leftMargin)/scale;
        //3.设置移动 缩放
        CGAffineTransform scaleForm=CGAffineTransformMakeScale(1, 1);//缩放
        CGAffineTransform translateForm=CGAffineTransformTranslate(scaleForm, translateX, 0); //移动
        self.showNavController.view.transform=translateForm;
        //4.设置一个按钮覆盖首页
//        UIButton *cover=[[UIButton alloc]initWithFrame:self.showNavController.view.bounds];
        cover.tag=buttonTag; //设置按钮的tag
        [cover addTarget:self action:@selector(coverClick:) forControlEvents:UIControlEventTouchUpInside];
        
        //创建一个平移手势对象，该对象可以调用handelPan：方法
//        UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handelPan:)];
//        [cover addGestureRecognizer:panGes];
        [self.showNavController.view addSubview:cover];
    }];

}

-(void)handelPan:(UIPanGestureRecognizer*)gestureRecognizer{
    
    NSLog(@"-->方法名--------》%s",__FUNCTION__);
    //获取平移手势对象在self.view的位置点，并将这个点作为self.aView的center,这样就实现了拖动的效果
//    CGPoint curPoint = [gestureRecognizer locationInView:self.view];
//    [self.aView setCenter:curPoint];
    
    // 获取手指移动的位置
    CGPoint trans = [gestureRecognizer translationInView:self.showNavController.view];
    
    self.showNavController.view.transform = CGAffineTransformTranslate(self.showNavController.view.transform, trans.x, 0);
}

#pragma mark 点击覆盖按钮返回
-(void)coverClick:(UIButton*)cover
{
    
    [UIView animateWithDuration:Timer animations:^{
        //设置左边菜单的动画属性
        CGAffineTransform scaleform=CGAffineTransformMakeScale(1, 1);
        CGAffineTransform anim=CGAffineTransformTranslate(scaleform, 0, 0);
        self.leftMenu.transform=anim;
        //设置右边view的动画属性
//        self.rightVc.backgroundView.transform=CGAffineTransformMakeScale(0.8, 0.8);
//        self.rightVc.view.alpha=0.5;
        
        self.showNavController.view.transform=CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [cover removeFromSuperview]; //移除按钮
    }];
}

#pragma mark 左边菜单栏按钮点击的代理方法
-(void)leftMenu:(SYLeftMenuView *)leftMenu didSelectedFrom:(NSInteger)from to:(NSInteger)to
{
    //1.取出上一个控制器
    SYBaseNavController *lastNav=self.childViewControllers[from];
    [lastNav.view removeFromSuperview];
    //2.要切换到的控制器
    SYBaseNavController *nav=self.childViewControllers[to];
    nav.view.transform=lastNav.view.transform; //赋值动画属性
    [self.view addSubview:nav.view];
    self.showNavController=nav;
    
    [self coverClick:(UIButton*)[nav.view viewWithTag:buttonTag]];
    
}

#pragma mark - 登陆
-(void)LeftUserClick:(id)sender
{
NSLog(@"-->小明明被点击了--------》%@",sender);
    
    UIViewController *vc = [[UIViewController alloc] init];//登陆控制器
    vc.view.backgroundColor = SYRandomColor;
    SYBaseNavController *nav=[[SYBaseNavController alloc]initWithRootViewController:vc];

    

    [self presentViewController:nav animated:YES completion:nil];
    
//    [self dismissViewControllerAnimated:YES completion:nil];//销毁视图

}



@end
